﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Media;
using TankWin.Classes;

namespace TankWin.Classes
{
    class GUIPlay:GUI
    {
        #region khai bao bien va hang
        int _Timer_Attacker = 300;
        const int _Timer_bullet = 1;

        const int _Map_Width = 52;
        const int _Map_Height = 52;
        const int _Tank_Width = 40;
        const int _Tank_Height = 40;
        const int _Window_Width = 640;
        const int _Window_Height = 640;
        const int _Board_Width = 520;
        const int _Board_height = 520;
        const int _Move = 10;
        const int _Move_Bullet = 10;
        const int _Timer_Effect = 150;//thời gian cần cho việc tạo hiệu ứng nổ 
        const int _Number_Attacker=10;//số lượng tank địch
        Point _Deffender_Position = new Point(173, 480);//khởi tạo vị trí tăng bảo vệ
        Point[] _Attacker_Position = new Point[3] { new Point(0, 0), new Point(240, 0), new Point(480, 0) };//khởi tạo vị trí tăng địch

        int _curNumAttacker = 0;//số lượng tăng hiện tại đang có
        int _maxAttacker = 13;//số lượng tăng nhiều nhất
        int _allAttacker = 0;//số lượng tăng xuất hiện
        static Random _rAttacker;
        //int[] _int = new int[2] { 1, 2 };
        //int _color = _rAttacker.Next(2);
        int _Score = 0;
        int _Kill = 0;
        int _Level = 1;
        String _Player = "";
        static int _MY_LIFE = 5;
        int _myLife = _MY_LIFE;
        const int _ADD = 10;

        public static ObjTank _Deffender;
        public static ObjTank[] _Attacker;
        public static ObjHeart _Heart;
        public static ObjIncLevel _IncreaseLevel;

        System.Windows.Forms.Timer _timerBullet;
        System.Windows.Forms.Timer _timerAttacker;

        SoundPlayer _explosion;
        SoundPlayer _you_lose;
        SoundPlayer _next_level;

        public static bool _hasSound = true;

        Bitmap _buffer;
        Bitmap _bufferMap;
        Random _random;

        public static bool _isPlayingGame = false;//danh dau la dang choi game va dang o GUIPlay
        public static bool _isAsking = false;//dung de danh dau la thuc hien thao tac you win hay you lose
        long _bullet_sta_time = System.DateTime.Now.Ticks;
        long _bullet_end_time = System.DateTime.Now.Ticks;
        long _att_sta_time = System.DateTime.Now.Ticks;
        long _att_end_time = System.DateTime.Now.Ticks;

        #endregion
        #region ham khoi tao
        public GUIPlay(Graphics _g)
        {
            _explosion = new SoundPlayer();
            _explosion.Stream = global::TankWin.ImageTank.enemy_explode;

            _you_lose = new SoundPlayer();
            _you_lose.Stream = global::TankWin.ImageTank.YouWillNeverWin;

            _next_level = new SoundPlayer();
            _next_level.Stream = global::TankWin.ImageTank.next_level;

            _random = new Random(System.DateTime.Now.Millisecond);
            this._Graphics = _g;
            _buffer = new Bitmap(_Window_Width, _Window_Height);
            _bufferMap = new Bitmap(_Window_Width, _Window_Height);

            _Player = Environment.UserName;

            DrawMap();

            _timerBullet = new System.Windows.Forms.Timer();
            _timerBullet.Interval = _Timer_bullet;
            _timerBullet.Tick += new EventHandler(TimerBullet_Tick);

            _timerAttacker = new System.Windows.Forms.Timer();
            _timerAttacker.Interval = _Timer_Attacker;
            _timerAttacker.Tick += new EventHandler(TimeAttacker_Tick);

            _Heart = new ObjHeart(new Point(240, 480));
            _IncreaseLevel = new ObjIncLevel(new Point(300, 90));
            _Deffender = new ObjTank(_Deffender_Position, EStatusDirect.Up, 0);

            _Attacker = new ObjTankAttacker[_maxAttacker];

            for (int i = 0; i < _maxAttacker; i++)
            {
                _Attacker[i] = null;
            }

            Point _p = _Attacker_Position[_random.Next(3)];

            _Attacker[0] = new ObjTankAttacker(new Point(480, 0), EStatusDirect.Down, 2);
            _Attacker[1] = new ObjTankAttacker(new Point(240, 0), EStatusDirect.Down, 2);
            _Attacker[2] = new ObjTankAttacker(new Point(0, 0), EStatusDirect.Down, 2);
            _Attacker[3] = new ObjTankAttacker(new Point(560, 0), EStatusDirect.Down, 2);
            _Attacker[4] = new ObjTankAttacker(new Point(160, 0), EStatusDirect.Down, 2);
            _Attacker[5] = new ObjTankAttacker(new Point(120, 0), EStatusDirect.Down, 2);
                
            _curNumAttacker = 6;
            _allAttacker = 6;
        }
        #endregion

        //khi  _timerbullet duoc goi
        public void TimerBullet_Tick(object obj, EventArgs e)
        {
            //dung de quan li thoi gian goi di chuyen
            _bullet_sta_time = System.DateTime.Now.Ticks;
            if (frMain._statusGUI != EStatusGUI.PlayGUI)
                return;
            ///////////////////////////Dan phe dich
            for (int i = 0; i < _maxAttacker; i++)
            {
                if(_Attacker[i]!=null)//tang dich duoc tao
                    if (_Attacker[i].Status == EStatusTank.IsAlive)//tang con song
                    {
                        if (_Attacker[i].Bullet.Status == EStatusTank.IsAlive)//dan con song
                        {
                            if (BulletCanMove(_Attacker[i].Bullet,false))//Di duoc
                            {
                                
                                _Attacker[i].Bullet.MoveFoward();
                            }
                            else //khong di duoc thi goi no
                            {
                                
                                _Attacker[i].Bullet.Destroy(_Graphics);
                                //PlaySound();
                                _Attacker[i].Bullet.Status = EStatusTank.IsDestroy;
                            }
                        }
                    }
            }
            //////////////////////////////////////////dan phe ta
            if (_Deffender.Bullet.Status == EStatusTank.IsAlive)
            {
                if (BulletCanMove(_Deffender.Bullet, true))//di duoc
                {
                    _Deffender.Bullet.MoveFoward();
                }
                else//khong di duoc
                {
                    _Deffender.Bullet.Destroy(_Graphics);
                    PlaySound();
                    _Deffender.Bullet.Status = EStatusTank.IsDestroy;
                }
            }
            //////////////////////////////////////////////////Kiem tra win va dung de len level
            if (_Kill >= _maxAttacker)
            {
                _isAsking = true;
                _timerAttacker.Stop();
                _timerBullet.Stop();

                if (_Timer_Attacker > 40 || ObjTankAttacker._Max_count > 2)//giup tang chay nhanh hon
                {
                    _Timer_Attacker -= 40;
                    ObjTankAttacker._Max_count -= 2;
                    _timerAttacker.Interval = _Timer_Attacker;
                }
                else
                {
                    DrawMessage("YOU WIN TANK GAME!!!", true);
                    Thread.Sleep(2000);
                    frMain._makeNewGame = true;
                    frMain._statusGUI = EStatusGUI.MainMenuGUI;
                }
                _Deffender.Position = _Deffender_Position;
                

                for (int i = 0; i < _maxAttacker; i++)
                {
                    _Attacker[i] = null;
                }
               
                _Level++;
               

                _next_level.Play();
                DrawMessage("Level: " + _Level, true);
                Thread.Sleep(2000);

                _Kill = 0;
                _allAttacker = 0;
                ObjMap.LoadMap(_Level);//

                _timerAttacker.Start();
                _timerBullet.Start();
                _isAsking = false;
                if (_Level == 11)
                {
                    DrawMessage("COMPLETED TEN LEVELS!!!", true);
                    Thread.Sleep(2000);
                    frMain._makeNewGame = true;
                    frMain._statusGUI = EStatusGUI.MainMenuGUI;
                    _timerBullet.Stop();
                    _timerAttacker.Stop();
                    _isPlayingGame = false;
                }
            }
            ////////////////////////////////////////ve sau khi xet dan bay
            if (_Graphics != null)
            {
                try
                {
                    Draw(_Graphics);
                }
                catch (System.Exception ex)
                {
                	
                }
            }
            ////////////////////////////////////////////Kiem tra xe tang ta co die khong
            if (_Deffender.Status == EStatusTank.IsDestroy)
            {
                _myLife--;
                DrawMap();
                if (_myLife > 0)
                {
                    //kiem tra ki
                    _Deffender.Position = _Deffender_Position;
                    _Deffender.Status = EStatusTank.IsAlive;
                }
                else
                {
                    _timerBullet.Stop();
                    _timerAttacker.Stop();
                    _isPlayingGame = false;

                    frMain._makeNewGame = true;
                    _you_lose.Play();
                    DrawMessage("YOU LOSE!!!", true);
                    Thread.Sleep(2000);
                    frMain._statusGUI = EStatusGUI.MainMenuGUI;

                }
            }
            ////////////////////////////////////////Kiem tra locot cua ta co die khong
            if (_Heart.Status != EStatusTank.IsAlive)
            {
                _timerAttacker.Stop();
                _timerBullet.Stop();
                _isPlayingGame = false;

                frMain._makeNewGame = true;
                _you_lose.Play();
                DrawMessage("YOU LOSE!", true);
                Thread.Sleep(2000);
                frMain._statusGUI = EStatusGUI.MainMenuGUI;

            }
        }
        //khi _timerAttacker duoc goi
        public void TimeAttacker_Tick(object obj, EventArgs e)
        {
            ///////////////////////
            if (_isAsking) return;
            //////////////dung cho viec quan li thoi gian khi di chuyen
            _att_sta_time = System.DateTime.Now.Ticks;
            //////////////////////////////////////////Neu chua tao du attacker thi tao them
            if (_curNumAttacker < _maxAttacker)//chua dam bao 5 con cung luc
            {
                if (_allAttacker < _maxAttacker)//chua het toi da so con
                {
                    Point _p = _Attacker_Position[_random.Next(3)];
                    Rectangle _rec1 = new Rectangle(_p.X, _p.Y, _Tank_Width, _Tank_Height);

                    bool _isGood = true;//
                    for (int i = 0; i < _maxAttacker; i++)
                    {
                        if(_Attacker[i]!=null)
                            if (_Attacker[i].Status == EStatusTank.IsAlive)
                            {
                                Rectangle _rec2 = new Rectangle(_Attacker[i].Position.X, _Attacker[i].Position.Y, _Tank_Width, _Tank_Height);
                                if (Collision2Rectangle(_rec1, _rec2))
                                {
                                    _isGood = false;
                                    break;
                                }
                            }
                    }
                    Rectangle _rec3 = new Rectangle(_Deffender.Position.X, _Deffender.Position.Y, _Tank_Width, _Tank_Height);
                    if (Collision2Rectangle(_rec1, _rec3))
                        _isGood = false;
                    Random random = new Random();
                    if (_isGood)
                    {
                        _Attacker[_curNumAttacker] = new ObjTankAttacker(_p, EStatusDirect.Down, 1);
                        _Attacker[_curNumAttacker].Status = EStatusTank.IsAlive;
                        _curNumAttacker++;
                        _allAttacker++;
                        DrawMap();
                    }
                }
            }
            ///////////////////////////////////////////////Xet xe tang dich di chuyen
            for (int i = 0; i < _maxAttacker; i++)
            {
                if(_Attacker[i]!=null)
                    if (_Attacker[i].Status == EStatusTank.IsAlive)
                    {
                        if (TankCanMove(_Attacker[i], false))
                        {
                            _Attacker[i].MoveFoward();
                        }
                        else //khong di chuyen duoc
                        {
                            _Attacker[i].Direct = (EStatusDirect)(_random.Next(4));
                        }
                    }
            }
        }

        ////////////////////////////////////////////////////
        //kiem tra vien dan co di chuyen duoc khong 
        //khong di chuyen duoc khi gap dan cua dich ,tuong.....
        ////////////////////////////////////////////////////
        private bool BulletCanMove(ObjBullet _bullet, bool _isofDeffender)
        {
            Point _nextPosition = _bullet.Position;
            switch (_bullet.Direct)
            {
                case EStatusDirect.Up:
                    _nextPosition.Y -= _Move_Bullet;
                    break;
                case EStatusDirect.Down:
                    _nextPosition.Y += _Move_Bullet;
                    break;
                case EStatusDirect.Left:
                    _nextPosition.X -= _Move_Bullet;
                    break;
                case EStatusDirect.Right:
                    _nextPosition.X += _Move_Bullet;
                    break;
            }
            int _X = _nextPosition.X;
            int _Y = _nextPosition.Y;


            /////////////////////neu la dan cua ta
            if (_isofDeffender)
            {
                for (int i = 0; i < _maxAttacker; i++)
                {
                    if (_Attacker[i] != null)//xe tang duoc tao
                        if (_Attacker[i].Status == EStatusTank.IsAlive)//con song
                        {
                            //////////////////////////////////////////////xet voi tang dich
                            int _l = _Attacker[i].Position.X;
                            int _r = _l + _Tank_Width;
                            int _t = _Attacker[i].Position.Y;
                            int _b = _t + _Tank_Height;
                            if (_X >= _l && _X <= _r && _Y >= _t && _Y <= _b)
                            {
                                _Attacker[i].Status = EStatusTank.IsDestroy;
                                //////////////////////////////
                                _Kill++;
                                _Score += _Level * _ADD;
                                if (frMain._highScore < _Score) frMain._highScore = _Score;
                                _curNumAttacker--;
                                DrawMap();
                                ///////////////////////////////
                            //}Rat quan trong 
                                ////////////////////////////////them dich de bu lai
                                {


                                    if (_allAttacker < _maxAttacker)
                                    {
                                        ////////////////////////////////////////
                                        Point _p = _Attacker_Position[_random.Next(3)];
                                        Rectangle _rec1 = new Rectangle(_p.X, _p.Y, _Tank_Width, _Tank_Height);
                                        bool _isGood = true;//danh dau xem co xe tang nao khong
                                        for (int j = 0; j < _maxAttacker; j++)
                                        {
                                            if (_Attacker[j] != null)
                                                if (_Attacker[j].Status == EStatusTank.IsAlive)
                                                {
                                                    Rectangle _rec2 = new Rectangle(_Attacker[j].Position.X, _Attacker[j].Position.Y, _Tank_Width, _Tank_Height);
                                                    if (Collision2Rectangle(_rec1, _rec2))
                                                    {
                                                        _isGood = false;
                                                        break;
                                                    }
                                                }
                                        }
                                        Rectangle _rec3 = new Rectangle(_Deffender.Position.X, _Deffender.Position.Y, _Tank_Width, _Tank_Height);
                                        if (Collision2Rectangle(_rec1, _rec3))
                                        {
                                            _isGood = false;
                                        }
                                        if (_isGood)
                                        {
                                            _Attacker[i].Status = EStatusTank.IsAlive;
                                            _Attacker[i].Position = _p;
                                            _curNumAttacker++;
                                            _allAttacker++;
                                        }
                                    }

                                }
                                return false;

                            }


                            ////////////////////////////////////////////xet voi dan dich
                            if (_Attacker[i].Bullet.Status == EStatusTank.IsAlive)//
                            {
                                if (_nextPosition == _Attacker[i].Bullet.Position)//trung toa do
                                {
                                    _Attacker[i].Bullet.Status = EStatusTank.IsDestroy;
                                    //khong can no nua
                                    return false;
                                }
                            }
                        }


                }
            }


                // }

                ///////////////dan cua dich
            else
            {
                //xet voi xe tang ta
                {

                    Rectangle _recc1 = new Rectangle(_Deffender.Position.X, _Deffender.Position.Y, _Tank_Width, _Tank_Height);
                    if (_recc1.Contains(_nextPosition))
                    {
                        _Deffender.Status = EStatusTank.IsDestroy;
                        return false;
                    }
                }

                if (_nextPosition == _Deffender.Bullet.Position)//xet voi dan ta
                {
                    _Deffender.Bullet.Status = EStatusTank.IsDestroy;
                    return false;
                }
                //return true;
                //}
            }

                #region ///////////////////xe voi tuong va locot
                //////////////////////////////////voi tuong
                if (_X <= 0 || _X >= _Board_Width || _Y <= 0 || _Y >= _Board_height)
                {
                    return false;
                }
                /////////////////////////////////voi locot
                Rectangle _rec = new Rectangle(_Heart.Position.X, _Heart.Position.Y, _Tank_Width, _Tank_Height);
                if (_rec.Contains(_bullet.Position))
                {
                    _Heart.Status = EStatusTank.IsDestroy;
                    return false;
                }
                #endregion
                #region /////////////////////////////////xet voi gach
                int _col1 = (_X / 10);
                int _row1 = (_Y / 10);

                int[] _cc = new int[4];
                int[] _rr = new int[4];

                switch (_bullet.Direct)
                {
                    case EStatusDirect.Up:
                        for (int i = 0; i < 4; ++i)
                        {
                            _rr[i] = _row1;
                        }
                        _cc[0] = _col1 - 2;//kho hieu the
                        _cc[1] = _col1 - 1;
                        _cc[2] = _col1;
                        _cc[3] = _col1 + 1;
                        break;
                    case EStatusDirect.Down:
                        goto case EStatusDirect.Up;
                    case EStatusDirect.Left:

                        for (int i = 0; i < 4; ++i)
                        {
                            _cc[i] = _col1;
                        }
                        _rr[0] = _row1 - 2;
                        _rr[1] = _row1 - 1;
                        _rr[2] = _row1;
                        _rr[3] = _row1 + 1;
                        break;
                    case EStatusDirect.Right:
                        goto case EStatusDirect.Left;

                }

                bool _all = false;

                if (ObjMap._map[_rr[1], _cc[1]] == 1 || ObjMap._map[_rr[2], _cc[2]] == 1)//dung lam gi
                    _all = true;

                if (_all)
                {
                    for (int i = 0; i < 4; ++i)
                    {
                        if (ObjMap._map[_rr[i], _cc[i]] == 1)
                            ObjMap._map[_rr[i], _cc[i]] = 0;
                    }
                    DrawMap(); // vẽ lại cái _bufferMap
                    _bullet.Status = EStatusTank.IsDestroy;
                    return false;
                }
                #endregion
                //////////////xe voi betong
                if (ObjMap._map[_row1, _col1] == 2) //trung betong
                {
                    return false;
                }
                return true;
            
        }
           
            
        
      /////////////////////////////////kiem tra xe tang co di duoc khong
        private bool TankCanMove(ObjTank _tank, bool _isDeffender)
        {
            int _l = _tank.Position.X;
            int _t = _tank.Position.Y;

            int _originL = _tank.Position.X;
            int _originT = _tank.Position.Y;

            switch (_tank.Direct)
            {
                case EStatusDirect.Up:
                    _t -= _Move;
                    break;
                case EStatusDirect.Down:
                    _t += _Move;
                    break;
                case EStatusDirect.Left:
                    _l -= _Move;
                    break;
                case EStatusDirect.Right:
                    _l += _Move;
                    break;
            }
            Rectangle _rec1 = new Rectangle(_l, _t, _Tank_Width, _Tank_Height);
            #region /////////////////////////////xet xe tang dich
            if (!_isDeffender)
            {
                int _Dx = _Deffender.Position.X;
                int _Dy = _Deffender.Position.Y;

                Rectangle _rec2 = new Rectangle(_Dx, _Dy, _Tank_Width, _Tank_Height);
                if (Collision2Rectangle(_rec1, _rec2))//cham
                {
                    return false;
                }


                ////////////////////////cham xe tang khac nhung cung la tang dich
                for (int j = 0; j < _maxAttacker; j++)
                {
                    if (_Attacker[j] != null)
                        if (_Attacker[j].Status == EStatusTank.IsAlive)
                            if (_tank != _Attacker[j])
                            {
                                Rectangle _rec3 = new Rectangle(_Attacker[j].Position.X, _Attacker[j].Position.Y, _Tank_Width, _Tank_Height);
                                if (Collision2Rectangle(_rec1, _rec3))
                                {
                                    return false;
                                }
                            }
                }
                
                /////////truong hop cham dan ta da su li trong ham BulletCanMove();

            }
            #endregion

            /////////////////////////////xet xe tang ta
            else
            {
                for (int i = 0; i < _maxAttacker; i++)
                {
                    if (_Attacker[i] != null)
                        if (_Attacker[i].Status == EStatusTank.IsAlive)
                        {
                            Rectangle _rec2 = new Rectangle(_Attacker[i].Position.X, _Attacker[i].Position.Y, _Tank_Width, _Tank_Height);
                            if (Collision2Rectangle(_rec1, _rec2))
                            {
                                return false;
                            }
                        }
                }
                //////////////////////////cham dan dich xu li trong BulletCanMOve()
            }

            ////////////////////cham lo cot,tuong.......
            Rectangle _recc3 = new Rectangle(_Heart.Position.X, _Heart.Position.Y, _Tank_Width, _Tank_Height);
            if (Collision2Rectangle(_rec1, _recc3))
            {
                return false;
            }
            /////////////////////////////cham vat len level
            Rectangle _recc4 = new Rectangle(_IncreaseLevel.Position.X, _IncreaseLevel.Position.Y, _Tank_Width, _Tank_Height);
            if (Collision2Rectangle(_rec1, _recc4))
            {
                _IncreaseLevel.Status = EStatusTank.IsDestroy;
                //_Level++;
            }
            //xet xe dich
            for(int i=0;i<_maxAttacker;i++)
            {
                if(_Attacker[i]!=null)
                    if(_Attacker[i].Status==EStatusTank.IsAlive&&_IncreaseLevel.Status==EStatusTank.IsAlive)
                    {
                        Rectangle _recc5 = new Rectangle(_Attacker[i].Position.X, _Attacker[i].Position.Y, _Tank_Width, _Tank_Height);
                        if (Collision2Rectangle(_recc4, _recc5))
                        {
                            return false;
                        }
                    }
            }
            
            //////////////////////////cham tuong
            if (_l < 0 || _l > (_Board_Width - _Tank_Width) || _t < 0 || _t > (_Board_height - _Tank_Height))
                return false;
            //////////////////////////////////////////cham gach va be tong
            int _col = _l / 10;//luon chia het vi_Move=10;
            int _row = _t / 10;
            switch (_tank.Direct)
            {
                case EStatusDirect.Up:
                    for (int i = 0; i < 4; i++)
                    {
                        if (ObjMap._map[_row, _col + i] == 1 || ObjMap._map[_row, _col + i] == 2)
                        {
                            return false;
                        }
                    }
                    break;
                case EStatusDirect.Down:
                    for (int i = 0; i < 4; i++)
                    {
                        if (ObjMap._map[_row + 3, _col + i] == 1 || ObjMap._map[_row + 3, _col + i] == 2)
                            return false;
                    }
                    break;
                case EStatusDirect.Left:
                    for (int i = 0; i < 4; i++)
                    {
                        if (ObjMap._map[_row + i, _col] == 1 || ObjMap._map[_row + i, _col] == 2)
                            return false;
                    }
                    break;
                case EStatusDirect.Right:
                    for (int i = 0; i < 4; i++)
                        if (ObjMap._map[_row + i, _col + 3] == 1 || ObjMap._map[_row + i, _col + 3] == 2)
                            return false;
                    break;

            }
            return true;
        }
#region  ////////////////////////////ve het moi thu
        public override void Draw(Graphics _G)
        {
            if (_isAsking) return;
            /////////////////////kiem tra xem neu tao game moi thi phai ve lai map
            if (frMain._makeNewGame == true)
            {
                DrawMap();
                frMain._makeNewGame = false;
            }
            ///////////////////////////////////////////////
            //kiem tra neu lan dau choi game thi phai khoi tao timer
            //////////////////////////////////////////////
            if (_isPlayingGame == false)
            {
                _isPlayingGame = true;
                _timerAttacker.Start();
                _timerBullet.Start();
            }
            /////////////bat dau ve/////////////////
            Graphics _gBuffer = Graphics.FromImage(_buffer);
            _gBuffer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            _gBuffer.Clear(Color.White);

            //ve ban do dia hinh
            _gBuffer.DrawImageUnscaled(_bufferMap, 0, 0);

            //ve lo cot
            _Heart.Draw(_gBuffer);

            //ve qua len level
            _IncreaseLevel.Draw(_gBuffer);

            //ve phe ta
            _Deffender.Draw(_gBuffer);
            _Deffender.Bullet.Draw(_gBuffer);

            //ve phe dich
            for (int i = 0; i < _maxAttacker; i++)
            {
                if(_Attacker[i]!=null)
                    if (_Attacker[i].Status == EStatusTank.IsAlive)
                    {
                        _Attacker[i].Draw(_gBuffer);
                        _Attacker[i].Bullet.Draw(_gBuffer);
                    }
            }
            ///////////////////ve buffer///////////////
            try
            {
                _Graphics.DrawImageUnscaled(_buffer, 0, 0);
            }
            catch (System.Exception ex)
            {
            	
            }
        }
#endregion
#region /////////////////////Ham nay se nhan vao key duoc gui tu frMain, va viec su li nhu the nao thi tuy vao GUI nay dung de lam gi
        public override void InputKey(Keys _key)
        {
            if (_isAsking)
            {
                _isAsking = false;
                return;
            }
            //Doan code nay de dam bao khi luon giu phim thi Attacker va bullet di chuyen duoc
            _att_end_time = System.DateTime.Now.Ticks;
            _bullet_end_time = System.DateTime.Now.Ticks;

            if (_att_end_time - _att_sta_time >= 10000 * _Timer_Attacker)
            {
                TimeAttacker_Tick(null, null);
                _att_sta_time = _att_end_time;
            }

            //di chuyen bullet
            if (_bullet_end_time - _bullet_sta_time >=_Timer_bullet)
            {
                TimerBullet_Tick(null, null);
                _bullet_sta_time = _bullet_end_time;

            }
            ////////////////////////////////////////////
            //trong ham nay hoan toan co the thay doi gia tri cua bien _status trong FrMain de co the thay doi GUI sang trang thai khac
            ////////////////////////////////////////////
            switch (_key)
            {
                case Keys.Up:
                    //_Deffender.Fire();//the best important @_@
                    if (_Deffender.Direct != EStatusDirect.Up)
                    {
                        _Deffender.Direct = EStatusDirect.Up;
                    }
                    if (TankCanMove(_Deffender, true))
                    {
                        _Deffender.MoveFoward();
                    }
                    break;
                    //goto case Keys.Space;//important.
                case Keys.Down:
                    if (_Deffender.Direct != EStatusDirect.Down)
                    {
                        _Deffender.Direct = EStatusDirect.Down;
                    }

                    if (TankCanMove(_Deffender, true))
                    {
                        _Deffender.MoveFoward();
                    }
                    break;
                case Keys.Left:
                    if (_Deffender.Direct != EStatusDirect.Left)
                    {
                        _Deffender.Direct = EStatusDirect.Left;
                    }

                    if (TankCanMove(_Deffender, true))
                    {
                        _Deffender.MoveFoward();
                    }
                    break;
                case Keys.Right:
                    if (_Deffender.Direct != EStatusDirect.Right)
                    {
                        _Deffender.Direct = EStatusDirect.Right;
                    }

                    if (TankCanMove(_Deffender, true))
                    {
                        _Deffender.MoveFoward();
                    }
                    break;
                case Keys.Space:
                    _Deffender.Fire();
                    break;

                case Keys.Escape:
                    _timerBullet.Stop();
                    _timerAttacker.Stop();
                    _isPlayingGame = false;
                    frMain._statusGUI = EStatusGUI.PauseMenuGUI;
                    break;
            }

         


        }
#endregion
        // hàm này vẽ lên cái _bufferMap, dùng cho vẽ Map
        private void DrawMap()
        {
            Graphics _gg = Graphics.FromImage(_bufferMap);

            Image _brick = global::TankWin.ImageTank.brick;
            Image _concrete = global::TankWin.ImageTank.concrete;
            Image _Tree = global::TankWin.ImageTank.Tree;

            //ObjMap _ObjMap = new ObjMap(_Level);

            _gg.Clear(frMain._Back_Color);
            _gg.DrawRectangle(new Pen(new SolidBrush(Color.White), 4), new Rectangle(-2, -2, _Board_Width + 5, _Board_height + 5));

            _gg.DrawRectangle(new Pen(new SolidBrush(Color.White), 4), new Rectangle(-2, 520 + 3, frMain._WINDOWS_WIDTH, frMain._WINDOWS_HEIGHT-_Board_height - 4));

            for (int i = 0; i < _Map_Height; ++i)
                for (int j = 0; j <_Map_Width; ++j)
                {
                    if (ObjMap._map[i, j] == 1)
                    {
                        _gg.FillRectangle(Brushes.DimGray, new Rectangle(j * 10 - 1, i * 10 - 1, 12, 12));
                        _gg.DrawImageUnscaled(_brick, j * 10, i * 10);
                    }
                    if (ObjMap._map[i, j] == 3)
                    {
                        _gg.DrawImageUnscaled(_Tree, j * 10, i * 10);
                    }
                    if (ObjMap._map[i, j] == 2)
                    {
                        _gg.DrawImageUnscaled(_concrete, j * 10, i * 10);
                    }
                    if (ObjMap._map[i, j] == 4)
                    {
                        _gg.FillRectangle(Brushes.DimGray, new Rectangle(j * 10 - 1, i * 10 - 1, 12, 12));
                        _gg.DrawImageUnscaled(_brick, j * 10, i * 10);
                    }
                   
                }
            // Vẽ điểm và các thông số khác...
            Font _font = new Font("Times New Roman", 18, FontStyle.Bold);
            Brush _br = Brushes.Silver;
            Brush _b1 = Brushes.DarkRed;

            _gg.DrawString("ATTACK", _font, _b1, new PointF(525, 20));
            _gg.DrawString("SCORE", _font, _b1, new PointF(525, 120));
            _gg.DrawString("KILL", _font, _b1, new PointF(525, 220));
            _gg.DrawString("LIFE", _font, _b1, new PointF(525, 320));
            _gg.DrawString("LEVEL", _font, _b1, new PointF(525, 420));

            _gg.DrawString("" + (_maxAttacker - _allAttacker), _font, _br, new PointF(525, 20 + 40));
            _gg.DrawString("" + _Score, _font, _br, new PointF(525, 120 + 40));
            _gg.DrawString("" + _Kill, _font, _br, new PointF(525, 220 + 40));
            _gg.DrawString("" + _myLife, _font, _br, new PointF(525, 320 + 40));
            _gg.DrawString("" + _Level, _font, _br, new PointF(525, 420 + 40));

            Font _f1 = new Font("Times New Roman", 24, FontStyle.Bold);
            _gg.DrawString("Player:", _f1, _br, new PointF(0, 525));
            Font _f = new Font("Times New Roman", 36, FontStyle.Bold);
            _gg.DrawString(_Player, _f, _br, new PointF(300 - 25 * _Player.Length / 2, 540));
        }

        private void DrawMessage(String _msg, bool _isWin)
        {
            Graphics _gBuff = Graphics.FromImage(_buffer);
            //_gBuff.Clear(Color.Black);
            _gBuff.FillRectangle(Brushes.Black, new Rectangle(0, 220, 640, 200));

            Pen _pen = new Pen(Color.Silver, 6);

            _gBuff.DrawRectangle(_pen, new Rectangle(0, 220, 640 - 6, 200 - 6));

            Font _font = new Font("Times New Roman", 30, FontStyle.Bold);
            _gBuff.DrawString(_msg, _font, Brushes.Red, new PointF(320 - 20 * _msg.Length / 2, 300));
            try
            {
                _isAsking = true;
                _Graphics.DrawImageUnscaled(_buffer, 0, 0);
            }
            catch { /** Trong bài này nếu xảy ra tranh chấp thì chấp nhận Crash chứ ko xử lý đồng bộ cho rắc rối **/}
        }

        const long _PLAY_TIME = 500;//?
        long _sound_sta_time = 0;
        long _sound_end_time;

        private void PlaySound()
        {
            if (_hasSound)
            {
                _sound_end_time = System.DateTime.Now.Ticks / 10000;
                if (_sound_end_time - _sound_sta_time >= _PLAY_TIME)
                {
                    _explosion.Play();
                    _sound_sta_time = System.DateTime.Now.Ticks / 10000;
                }
            }
        }


        #region Kiểm tra hai hình chữ nhật lồng nhau ...
        // xét hai hình chữ nhật có giao nhau ko
        private bool Collision2Rectangle(Rectangle _rec1, Rectangle _rec2)
        {
            Rectangle rectangle3 = new Rectangle();
            if (_rec1.IntersectsWith(_rec2))
            {
                rectangle3 = Rectangle.Intersect(_rec1, _rec2);
                if (!rectangle3.IsEmpty)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

    }
}
